﻿using SnmpForm.APConfig;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SnmpForm.ApBatchConfig
{
    public partial class FrmUpdateStatus : FrmApBatchConfig
    {
        private string m_strRequest = null;
        public FrmUpdateStatus(List<BusinessWrapper.Ap> list, string strRequest)
            :base(list)
        {
            m_strRequest = strRequest;
            InitializeComponent();
        }

        public delegate void UpdateSystemStatus(BusinessWrapper.Ap ap, string status);
        public UpdateSystemStatus updateSystemStatus;
        public void UpdateSystemStatusMethod(BusinessWrapper.Ap ap, string strUpdateStatus)
        {
            foreach (ListViewItem item in listViewStatus.Items)
            {
                int nApid = Convert.ToInt32(item.Text);
                if (nApid == ap.GetInfo().id)
                {
                    bool bChange = 
                        !item.SubItems[2].Text.Equals(strUpdateStatus);
                    if (bChange)
                    {
                        item.SubItems[2].Text = strUpdateStatus;
                    }
                    break;
                }
            }
        }

        private Thread objThreadApUpdateSystemStatus = null;
        private BatchUpdateSystemTask taskApUpdateSystemStatus = null;

        private void StartApUpdateSystemTask()
        {
            if (objThreadApUpdateSystemStatus == null ||
                objThreadApUpdateSystemStatus.ThreadState == ThreadState.Stopped)
            {
                taskApUpdateSystemStatus =
                    new BatchUpdateSystemTask(this, m_listAsset, m_strRequest);
                objThreadApUpdateSystemStatus = new Thread(taskApUpdateSystemStatus.Run);
                objThreadApUpdateSystemStatus.Start();
            }
        }

        private void StopApUpdateSytemTask()
        {
            if (objThreadApUpdateSystemStatus != null)
            {
                if (objThreadApUpdateSystemStatus.ThreadState != ThreadState.Stopped)
                {

                    objThreadApUpdateSystemStatus.Abort();
                    objThreadApUpdateSystemStatus = null;
                }
            }
        }

        private void FrmUpdateStatus_Load(object sender, EventArgs e)
        {
            //listView
            listViewStatus.View = View.Details;
            listViewStatus.FullRowSelect = true;
            listViewStatus.Columns.Add("id", 50);
            listViewStatus.Columns.Add("标示码", 130);
            listViewStatus.Columns.Add("状态", 250);
            UpdateViewStatus();

            updateSystemStatus = new UpdateSystemStatus(UpdateSystemStatusMethod);
            StartApUpdateSystemTask();
        }

        private void UpdateViewStatus()
        {
            foreach (BusinessWrapper.Ap ap in m_listAsset)
            {
                ListViewItem lvi = new ListViewItem();

                DbWrapper.DbDataTypes.Ap apinfo = ap.GetInfo();
                lvi.Text = apinfo.id.ToString();
                lvi.SubItems.Add(apinfo.mark);
                lvi.SubItems.Add("等待升级");

                listViewStatus.Items.Add(lvi);
            }
        }

        private void FrmUpdateStatus_FormClosing(object sender, FormClosingEventArgs e)
        {
            StopApUpdateSytemTask();
        }
    }
}
